package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"zhanyan/pac/util"
	"zhanyan/service/seat_service"
)

// GetSeats
// @Summary      获取一个班级中的所有座位
// @Description  获取一个班级中的所有座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Param        id   query     int  true  "教室id"
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/getSeats [get]
func GetSeats(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	seat := seat_service.Seat{ClassroomID: id}
	getSeats, err := seat.GetSeats()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	marshal := util.Marshal(getSeats)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  marshal,
	})
}

// StudentIsBind
// @Summary      学生是否关联座位
// @Description  学生是否关联座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Param        Seat   json    true  "学生"
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/studentIsBind [get]
func StudentIsBind(c *gin.Context) {
	sid, _ := strconv.Atoi(c.Query("id"))
	seat := seat_service.Seat{StudentID: sid}
	bind, err := seat.StudentIsBind()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  bind,
	})
}

// AddOnClass
// @Summary      在教室中添加座位
// @Description  在教室中添加座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Param        Seat   json object      true  "X轴"
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/addOnClass [post]
func AddOnClass(c *gin.Context) {
	seat := seat_service.Seat{}
	err := c.ShouldBindJSON(&seat)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	err = seat.AddOnClassroom()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
	})
}

// AddOnStudent
// @Summary      学生关联座位
// @Description  学生关联座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Param        Seat   json    true  "学生"
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/addOnStudent [post]
func AddOnStudent(c *gin.Context) {
	seat := seat_service.Seat{}
	err := c.ShouldBindJSON(&seat)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	err = seat.AddOnStudent()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "绑定成功",
	})
}

// UpdateOnStudent
// @Summary      修改学生关联座位
// @Description  修改学生关联座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Param        seat   json      int  true  "学生"
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/updateOnStudent [put]
func UpdateOnStudent(c *gin.Context) {
	seat := seat_service.Seat{}
	err := c.ShouldBindJSON(&seat)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	err = seat.UpdateOnStudent()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改绑定成功",
	})
}

// DeleteOnStudent
// @Summary      学生删除关联座位
// @Description  学生删除关联座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/deleteOnStudent/:id [delete]
func DeleteOnStudent(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	seat := seat_service.Seat{StudentID: id}
	err := seat.DeleteOnStudent()
	if err != nil {
		c.JSON(203, err.Error())
		return
	}
	c.JSON(200, "success")
}

// DeleteOnClassroom
// @Summary      清除教室所有座位
// @Description  清除教室所有座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/deleteOnClassroom/:id [delete]
func DeleteOnClassroom(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	seat := seat_service.Seat{ClassroomID: id}
	err := seat.DeleteSeatOnClassroom()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "清空成功",
	})
}

// DeleteOneSeat
// @Summary      删一个座位
// @Description  删一个座位
// @Tags         seat
// @Accept       json
// @Produce      json
// @Success      200  {object}  map[string]interface{}
// @Router       /seat/deleteOneSeat/:id [delete]
func DeleteOneSeat(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	seat := seat_service.Seat{ID: id}
	err := seat.DeleteOneSeat()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusNoContent,
			"msg":  err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
	})
}
